package com.cheng.rebuildproject.config.jedis;



import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;


/**
 * @author 程昆
 **/
@Slf4j
@Configuration
public class JedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;



    @Bean
    public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.max-active}") int maxActive,
                                           @Value("${spring.redis.jedis.pool.max-idle}") int maxIdle,
                                           @Value("${spring.redis.jedis.pool.max-wait}") int minIdle,
                                           @Value("${spring.redis.jedis.pool.min-idle}") long maxWaitMillis) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWait(Duration.ofDays(maxWaitMillis));
        return jedisPoolConfig;
    }


    @Bean
    public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig){

        JedisPool jedisPool=new JedisPool(jedisPoolConfig,host,port);

        log.info("JedisPool连接成功:"+host+"\t"+port);

        return jedisPool;
    }
}
